package com.zhu.nio.channel;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by ehsy_it on 2016/4/12.
 */
public class ScatteringRead {

    public static void main(String[] args) throws IOException{
        RandomAccessFile file = new RandomAccessFile("d:/data/nio-data.txt", "rw");
        FileChannel channel = file.getChannel();

        ByteBuffer buf1 = ByteBuffer.allocate(4);
        ByteBuffer buf2 = ByteBuffer.allocate(5);
        ByteBuffer buf3 = ByteBuffer.allocate(6);

        ByteBuffer[] bufArr = {buf1, buf2,buf3};

        channel.read(bufArr);

        for(ByteBuffer buf : bufArr){
            buf.flip();
            System.out.print("buf:");
            while(buf.hasRemaining()){
                System.out.print((char)buf.get());
            }
            System.out.println();
        }

        file.close();
    }
}
